<? 
session_start(); 
include("include/db_connect.php"); 
connect();
$sql="SET CHARACTER SET UTF8";   
query($sql); 
include "try_session_login.php";

$page = (isset($_GET['page']))? intval($_GET['page']) : 1;
$limit_end=50;
$limit_start = ($page-1)*$limit_end;
$send = "id".$_GET['id'];

$sql = "SELECT 
			mbs.*,bnf.up_id,bnf.down_id,bnf.level,SUM(tb_report.insureTotal) as price
		FROM 
			tb_benefi as bnf,tb_membership as mbs
		LEFT JOIN
			tb_report
		ON
			mbs.username = membershipId
		WHERE
			mbs.id = bnf.down_id
		AND
			bnf.level = '".$_GET['level']."'
		AND
			bnf.up_id = '".$_GET['id']."' 
		GROUP BY
			down_id
		ORDER BY
			mbs.username asc
		";
$qr = select($sql. " LIMIT " . $limit_start. "," . $limit_end);
$rows = mysql_num_rows(mysql_query($sql));
$total = count($qr);
$i=0;


?>
<script>
$(function(){
	$("#create").click(function(){
			$("#dialog_create").dialog({
				modal: true,
				buttons: {
					ตกลง: function(){
							$.post('member_control.php',$('#form2').serialize(),function(data){
									if(data!=''){
										$('#msg').html(data);
										$("#dialog").dialog({modal: true,buttons: {ตกลง: function() {$("#dialog").dialog( "close" );}}});
									}else{
										$("#dialog_create").dialog( "close" );
										$('#msg').html('เพิ่มข้อมูลสำเร็จ');
										$("#dialog").dialog({
											modal: true,
											buttons: {
												ตกลง: function() {
														$("#dialog").dialog( "close" );
														loadpage();
													}
											}
										});
										
										//loadpage();
									}
							});
						},
					ยกเลิก:function() {
							$( this ).dialog( "close" );
						}
				}
			});
		});

	 fgbutton();
});

function page(page)
{
	$('#ui-tabs-1').html('<img style=\'margin-top:100px;\' src=\'images/loading.gif\' align="absmiddle"/>'); 
	$.get(
			'member_benefi_ajax.php',
			{
				page:page,
				level:'<?=$_GET['level']?>',
				id:'<?=$_GET['id']?>'
			},
			function(data){
				$('#ui-tabs-<?=$_GET['level']?>').html(data).hide().fadeIn('slow');
			},
			'html'
		);	
}
</script>
<div style="clear:both;"></div>
        <table width="100%" border="1" align="center" cellpadding="3" cellspacing="0" style="border-collapse:collapse;" id="tbl_content">
          <tr class="ui-widget-header">
            <td width="64" align="center" >ลำดับ</td>
            <td width="261" align="center"  >รหัสประจำตัว</td>
            <td width="244" align="center"  >ชื่อ-นามสกุล</td>
            <td width="264" align="center"  >เบอร์โทรศัพท์</td>
            <td width="133" align="center"  >อีเมลล์</td>
            <td width="187" align="center"  >รายได้สุทธิ</td>
          <!--  <td align="center" >ลบ</td>-->
          </tr>
          <?php
          
        if($total > 0){ 
            while($i<count($qr)){ 
            $rs_ref=$qr[$i];
			if($bg=='#fff'){$bg='#f2f2f2';}else{$bg='#fff';}
        ?>
          <tr  bgcolor="<?=$bg?>"   onmouseout="this.className=''" onmouseover="this.className='ui-state-error'" id="list<?=$rs_ref['id']?>">
            <td align="center" ><?=$limit_start+$i+1;?></td>
            <td align="center"><?=$rs_ref['username']?></td>
            <td align="center"><? echo $rs_ref['name'] ?></td>
            <td align="center"><?=$rs_ref['phone']?></td>
            <td align="center"><?=$rs_ref['email']?></td>
            <td align="center" valign="middle"> <?php echo number_format($rs_ref['price']);  ?></td>
           <!-- <td width="30" align="center" valign="middle">
                <ul id="icons" class="ui-widget ui-helper-clearfix">
                    <li class="ui-state-default ui-corner-all" title="ลบรายชื่อ" onclick="del('<?=$rs_ref['id']?>')"><span class="ui-icon ui-icon-trash"></span></li>
                 </ul>
             </td>-->
          </tr>
          <?php 
			  $i++;
				}//end while//
			}//end if($total > 0)//
          ?>
        </table><br />
        <?php 
		if(($rows/$limit_end) > 1){ 
		
			echo "\n<div align=\"right\">" . page_navi2($page, $limit_end, 4, $rows, $send,"mynavic","mynavi") . " </div>";
		
		}
	?>   
      